package com.cloud.common.core.model.supers;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.common.core.cons.HttpCode;
import com.cloud.common.core.cons.PageCons;
import com.cloud.common.core.model.response.SuccessResponses;
import com.cloud.common.core.utils.TypeUtils;
import com.cloud.common.core.utils.safe.AntiSQLFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * SuperController
 *
 * @author yan
 */
public class SuperController {

    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    protected HttpServletRequest request;

    @Autowired
    protected HttpServletResponse response;

    @Autowired
    protected HttpSession session;

    /**
     * 获取完整的请求路径，包括：域名，端口，上下文访问路径
     *
     * @return 服务地址
     */
    public String getWholeUrl() {
        StringBuffer url = request.getRequestURL();
        String contextPath = request.getServletContext().getContextPath();
        return url.delete(url.length() - request.getRequestURI().length(), url.length()).append(contextPath).toString();
    }

    /**
     * 成功返回
     *
     * @param object
     * @return
     */
    public <T> SuccessResponses<T> success(T object) {
        return SuccessResponses.<T>success(response, object);
    }

    /**
     * 成功返回
     *
     * @return
     */
    public SuccessResponses<Void> success() {
        return success(HttpCode.SC_OK);
    }

    /**
     * 成功返回
     *
     * @param status
     * @param object
     * @return
     */
    public <T> SuccessResponses<T> success(int status, T object) {
        return SuccessResponses.<T>success(response, status, object);
    }


    /**
     * 成功返回
     *
     * @param status
     * @return
     */
    public SuccessResponses<Void> success(int status) {
        return SuccessResponses.<Void>success(response, status);
    }


    /**
     * 获取分页对象
     *
     * @return
     */
    protected <T> Page<T> getPage() {
        return getPage(false);
    }

    /**
     * 获取分页对象
     *
     * @param openSort
     * @return
     */
    protected <T> Page<T> getPage(boolean openSort) {
        int index = 1;
        // 页数
        Integer cursor = TypeUtils.castToInt(request.getParameter(PageCons.PAGE_PAGE), index);
        // 分页大小
        Integer limit = TypeUtils.castToInt(request.getParameter(PageCons.PAGE_ROWS), PageCons.DEFAULT_LIMIT);
        // 是否查询分页
        Boolean searchCount = TypeUtils.castToBoolean(request.getParameter(PageCons.SEARCH_COUNT), true);
        limit = limit > PageCons.MAX_LIMIT ? PageCons.MAX_LIMIT : limit;
        Page<T> page = new Page<>(cursor, limit, searchCount);
        if (openSort) {
            page.setAsc(getParameterSafeValues(PageCons.PAGE_ASCS));
            page.setDesc(getParameterSafeValues(PageCons.PAGE_DESCS));
        }
        return page;
    }

    /**
     * 获取页数
     *
     * @return
     */
    protected Integer getCursor() {
        int index = 1;
        return TypeUtils.castToInt(request.getParameter(PageCons.PAGE_PAGE), index);
    }

    /**
     * 获取分页大小
     *
     * @return
     */
    protected Integer getLimit() {
        return TypeUtils.castToInt(request.getParameter(PageCons.PAGE_ROWS), PageCons.DEFAULT_LIMIT);
    }

    /**
     * 获取安全参数(SQL ORDER BY 过滤)
     *
     * @param parameter
     * @return
     */
    protected String[] getParameterSafeValues(String parameter) {
        return AntiSQLFilter.getSafeValues(request.getParameterValues(parameter));
    }
}
